from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from base_tools.my_looger import logger
from config import *

def init_driver(url, des):
        driver = webdriver.Remote(url, des)
        return driver

class BasePage():

        def __init__(self, driver):
                # 实例化驱动对象
                self.driver = driver
                # self.driver = webdriver.Remote()
                # 设置隐式等待：10
                self.driver.implicitly_wait(10)

        def my_find_element(self, ele, time_out=10):
                """
                使用显示等待查找元素
                :param ele:
                :return:
                """
                logger.info("查找元素：{}，等待时长：{}".format(ele, time_out))
                self.element = WebDriverWait(self.driver, time_out, 0.5).until(EC.presence_of_element_located(ele))

        def my_click(self, ele, **kwargs):
                """
                点击元素
                :param ele:
                :param kwargs:
                :return:
                """
                self.my_find_element(ele, **kwargs)
                logger.info("点击此元素。。。。")
                self.element.click()

        def my_send_keys(self, ele, value, **kwargs):
                """
                元素内输入文本
                :param ele:
                :param value:
                :param kwargs:
                :return:
                """
                self.my_find_element(ele, **kwargs)
                logger.info("元素内输入文本：{}".format(value))
                self.element.send_keys(value)

        def my_get_text(self, ele, **kwargs):
                self.my_find_element(ele, **kwargs)
                logger.info("获取元素内文本值。。。")
                return self.element.text

        def my_back_index(self):
                """
                返回首页
                :return:
                """
                while True:
                        # 判断当前所在包名和页面名是不是我们的首页
                        app_package = self.driver.current_package
                        app_activity = self.driver.current_activity
                        if app_package == des.get("appPackage"): # 当前所在包是企业微信这个app
                                if app_activity == des.get("appActivity"): # 当前所在页面是首页
                                        break
                                else:
                                        self.driver.back()
                        else:   # 当前不在企业微信这个app中，需要重新进入企业微信
                                self.driver.start_activity(des.get("appPackage"), des.get("appActivity"))

        def my_quit(self):
                self.driver.quit()

# des = {
#         "platformName": "Android",
#         "platformVersion": "7.1.2",
#         "deviceName": "emulator-5554",
#         "appPackage": "com.android.settings",
#         "appActivity": ".Settings"
#     }
#
# url = "http://127.0.0.1:4723/wd/hub"
#
# from config import *
# driver = init_driver(url, des)
# page_obj = BasePage(driver)
# import time
# time.sleep(5)
# page_obj.my_quit()